package cn.zs.groupmarket.domain.activity.service;

import cn.zs.groupmarket.domain.activity.adapter.repository.IActivityRepository;
import cn.zs.groupmarket.domain.activity.model.entity.MarketProductEntity;
import cn.zs.groupmarket.domain.activity.model.entity.TrialBalanceEntity;
import cn.zs.groupmarket.domain.activity.model.entity.UserGroupBuyOrderDetailEntity;
import cn.zs.groupmarket.domain.activity.model.valobj.TeamStatisticVO;
import cn.zs.groupmarket.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import cn.zs.groupmarket.types.design.framework.tree.StrategyHandle;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 首页营销服务
 * @Auther: zs
 * @Date: 2024/12/21/16:47
 */
@Service
public class IIndexGroupBuyMarketServiceImpl implements IIndexGroupBuyMarketService{
    @Resource
    private IActivityRepository repository;
    @Resource
    private DefaultActivityStrategyFactory defaultActivityStrategyFactory;
    @Override
    public TrialBalanceEntity indexMarketTrial(MarketProductEntity marketProductEntity) throws Exception {
        StrategyHandle<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> handle = defaultActivityStrategyFactory.strategyHandle();


//
        TrialBalanceEntity trialBalanceEntity = handle.apply(marketProductEntity, new DefaultActivityStrategyFactory.DynamicContext());
//
        return trialBalanceEntity;
//        return defaultActivityStrategyFactory.strategyHandle().apply(marketProductEntity, new DefaultActivityStrategyFactory.DynamicContext());
    }

    @Override
    public List<UserGroupBuyOrderDetailEntity> queryInProgressUserGroupBuyOrderDetailList(Long activityId, String userId, int ownerCount, int randomCount) {
        List<UserGroupBuyOrderDetailEntity> unionAllList = new ArrayList<>();
        // 查询个人拼团数据
        if (0 != ownerCount) {
            List<UserGroupBuyOrderDetailEntity> ownerList = repository.queryInProgressUserGroupBuyOrderDetailListByOwner(activityId, userId, ownerCount);
            if (null != ownerList && !ownerList.isEmpty()){
                unionAllList.addAll(ownerList);
            }
        }
        // 查询其他非个人拼团
        if (0 != randomCount) {
            List<UserGroupBuyOrderDetailEntity> randomList = repository.queryInProgressUserGroupBuyOrderDetailListByRandom(activityId, userId, randomCount);
            if (null != randomList && !randomList.isEmpty()){
                unionAllList.addAll(randomList);
            }
        }

        return unionAllList;
    }

    @Override
    public TeamStatisticVO queryTeamStatisticByActivityId(Long activityId) {
        return repository.queryTeamStatisticByActivityId(activityId);
    }
}
